/*
sq880.c
offer sq880 motor driver control function realize
designed by lunanting

---------------------------
2020-07-30 09:55:51
create file
---------------------------

*/
#include "sq880.h"
#include "../../../1km/bsp/modbus_485.h"
#include "../../../1km/bsp/cli_cmd.h"

static SQ880_class sq880={0};

int xp_sq880_write(u16 addr,u16 data);

/*
init sq880
param:
    port:uart port
    speed:uart speed
    u8:slave id
return:
    0=success
*/
int xp_sq880_init(u8 port,int speed,u8 slave_id){
    if(slave_id<2||slave_id>127){
        println("warn:slave id set error,id=%d,reset id as 127",slave_id);
        slave_id=127;
    }
    sq880.port=port;
    sq880.speed=speed;
    sq880.slave_id=slave_id;
    if(xp_modbus_init(port,speed,MB_PAR_NONE,100)!=0){
        println("modbus init fail~");
        return -1;
    }
    //set F00.01=2
    if(xp_sq880_write(0x0001,2)!=0)return -2;
    //set F00.06=8
    if(xp_sq880_write(0x0006,8)!=0)return -3;
    //set default frequency = 30hz
    // xp_sq880_set_freq(30);
    sq880.init=1;
    println("sq880 init success~");
    return 0;
}

/*
write data to driver
param:
    addr:reginter address
    data:wite data
return:
    0=success
*/
int xp_sq880_write(u16 addr,u16 data){
    int sta=0;
    MB_data modbus;
    u8 rec_data[10];
    u8 rec_len;
    u8 count=5;

    modbus.slave_id=sq880.slave_id;
    modbus.mb_fun=mb_single_w;
    modbus.start_addr=addr;
    modbus.wBuf=&data;
    modbus.rwLen=1;
    modbus.resBuf=rec_data;
    modbus.resLen=&rec_len;
    while(count--){
        if(xp_modbus_ask(sq880.port,&modbus)==0)return 0;
    }
    return -1;
}



/*
set motor driver run state
param:
    sta:SQ880_run_type
return:
    0=success
*/
int xp_sq880_set_run(SQ880_run_type sta){
    u16 data=(u16)sta;
    if(data<1||data>8){
        println("input sta error!sta=%d",data);
        return -2;
    }
    
    return xp_sq880_write(0x2000,data);
}


/*
set motor driver run frequency
param:
    freq:frequency hz
return:
    0=success
*/
int xp_sq880_set_freq(u16 freq){
    u16 data=freq*100;

    return xp_sq880_write(0x3000,data);
}


/*
test brush blowing thread
吹五分钟，停五分钟，循环测试

*/
void xp_sq880_blowing_test(void *arg){

    while(1){
        println("dryerl blow start~~");
        xp_sq880_set_run(sq880_fw);
        aos_msleep(60*1000*5);
        println("drerl blow stop~~");
        xp_sq880_set_run(sq880_slow_stop);
        aos_msleep(60*1000*5);
    }
}


/*************************debug****************************/
/*
sq880 debug
*/
int xp_sq880_debug(char *type,char *fun,char *param){
    if(strcmp(type,"sq880")!=0)return 0;
    if(strcmp(fun,"set_run")==0){
        xp_sq880_set_run((SQ880_run_type)atoi(param));
    }
    else if(strcmp(fun,"set_freq")==0){
        xp_sq880_set_freq(atoi(param));
    }
    else if(strcmp(fun,"blow_test")==0){
        if(strcmp(param,"start")==0){
            aos_task_new("blow test",xp_sq880_blowing_test,NULL,2100);
        }
        else if(strcmp(param,"stop")==0){
            aos_task_delete("blow test");
            xp_sq880_set_run(sq880_slow_stop);
        }
    }

    else{
        return 0;
    }
    return 1;
}


//end of the file
